<?php

/*
  -----------------------------------------------------------------
  Copyright (c) 2010 25Solutions   All Rights Reserved.

  This software is the confidential and proprietary information
  of 25Solutions ("Confidential Information").
  You shall not disclose such Confidential Information and shall
  use it only in accordance with the terms of the license
  agreement you entered into with 25Solutions
  -----------------------------------------------------------------
 */

/**
 * Description of TagcloudHelper
 *
 * @author fabio.lorenzini@25solutions.com
 * based on
 * http://snippets.symfony-project.org/snippet/240
 */
class TagcloudHelper {

    /**
     * Generate the tag array and echo the cloud
     *
     * @return array
     *
     */
    public static function showCloud($tags) {
//        asort($tags);
//        reset($tags);
        $smallest = current($tags);
        end($tags);
        $largest = current($tags);
        $diff = $largest - $smallest;
        $interval = round($diff / 3);

        reset($tags);

        $cloud_tags = array();
        foreach ($tags as $tag => $amount) {
            echo '<span class='.TagcloudHelper::getSize($amount, $smallest,$largest, $interval ).'>'.$tag.'</span> ';
        }
    }

    /**
     * Get the size (css class) based on the amount
     *
     * @param integer $amount
     * @return array
     */
    private static function getSize($amount, $smallest, $largest, $interval ) {
        if ($amount == $smallest) {
            return 'cloud_xsmall';
        }
        if ($amount == $largest) {
            return 'cloud_xlarge';
        }
        if ($amount > $smallest + (2 * $interval)) {
            return 'cloud_large';
        }
        if ($amount > $smallest + $interval) {
            return 'cloud_medium';
        }
        return 'cloud_small';
    }

}
?>
